﻿package component.fileupload.listener;

/**
 * 上传文件的监听实现类
 */
public class UploadFileListener implements IOutputStreamListener {
	// 保存状态的内部类对象
	private FileUploadStatus fileUploadStatus = new FileUploadStatus();

	// 构造方法
	public UploadFileListener(long totalSize) {
		fileUploadStatus.setTotalSize(totalSize);
	}

	public void start() {
		// 设置当前状态为开始
		fileUploadStatus.setCurrentStatus("start");
		System.out.println("开始上传");
	}

	public void bytesRead(int byteCount) {
		// 将已读取的数据保存到状态对象中
		fileUploadStatus.incrementBytesRead(byteCount);
		// 设置当前的状态为读取过程中
		fileUploadStatus.setCurrentStatus("reading");
		// System.out.println("读取数据");
	}

	public void error(String s) {
		// 设置当前状态为出错
		fileUploadStatus.setCurrentStatus("error");
		System.out.println("上传出错");
	}

	public void done() {
		// 设置当前已读取数据等于总数据大小
		fileUploadStatus.setBytesRead(fileUploadStatus.getTotalSize());
		// 设置当前状态为完成
		fileUploadStatus.setCurrentStatus("done");
		System.out.println("上传完成");
	}

	public Object getState() {
		// 返回当前状态对象
		return fileUploadStatus;
	}

}
